//
//  LLT_DataSender_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/28.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_DataSender_LXH.h"
#import "LLT_NewsModel.h"
#import "LLT_PersonModel.h"
#import "LLT_VideoModel.h"
#import "LLT_LiveModel.h"
#import "AFNetworking.h"

@interface LLT_DataSender_LXH()

@property (nonatomic, strong) NSMutableDictionary *dictionary;

@end

@implementation LLT_DataSender_LXH

/**
 * 懒加载
 */
-(NSMutableDictionary *)dictionary
{
    if (!_dictionary) {
        _dictionary = [NSMutableDictionary new];
    }
    return _dictionary;
}

/**
 * 获取到新闻的数组
 */
-(void)getNewsArrayByCount:(NSInteger)count
{
    //生成URL
    NSString *urlString = kNewsURL(count);
    
    NSMutableArray *newsArray = [NSMutableArray new];
    
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [myManager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSDictionary *dictionary = [self dictionaryWithJsonString:operation.responseString];
        
        NSArray *news = dictionary[@"result"];
    
        for (NSDictionary *dic in news)
        {
            LLT_NewsModel *newsModel = [[LLT_NewsModel alloc]init];
            [newsModel setValuesForKeysWithDictionary:dic];
            [newsArray addObject:newsModel];
        }
        if (self.sendNewsArray)
        {
            self.sendNewsArray(newsArray);
        }


    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        //NSLog(@"%@",error);
        
    }];
    
}

/**
 * 获取到直播的数组
 */
-(void)getLiveArrayByCount:(NSInteger)count
{
    //生成URL
    NSString *urlString = kLiveURL(count);
    
    NSMutableArray *liveArray = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [myManager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSDictionary *dictionary = [self dictionaryWithJsonString:operation.responseString];
        
        NSArray *lives = dictionary[@"result"];
        
        for (NSDictionary *dic in lives)
        {
            LLT_LiveModel *liveModel = [[LLT_LiveModel alloc]init];
            [liveModel setValuesForKeysWithDictionary:dic];
            [liveArray addObject:liveModel];
        }
        if (self.sendLiveArray)
        {
            self.sendLiveArray(liveArray);
        }
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        //NSLog(@"%@",error);
        
    }];
    
}

/**
 * 获取解说数组
 */
-(void)getPersonArrayByCount:(NSInteger)count
{
    //生成URL
    NSString *urlString = kVideosURL;
    
    NSMutableArray *personArray = [NSMutableArray array];
    
    NSMutableDictionary *personDictionary = [NSMutableDictionary dictionary];
    
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [myManager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        NSDictionary *dictionary = [self dictionaryWithJsonString:operation.responseString];
        
        NSArray *array = dictionary[@"result"];
        
        for (NSDictionary *dict in array) {
            NSString *key = dict[@"title"];
            NSMutableArray *arr = personDictionary[key];
            if (arr == nil) {
                arr = [NSMutableArray array];
            }
            NSArray *dics = dict[@"items"];
            for (NSDictionary *di in dics) {
                LLT_PersonModel *p = [[LLT_PersonModel alloc]init];
                [p setValuesForKeysWithDictionary:di];
                [arr addObject:p];
            }
            [personArray addObject:key];
            [personDictionary setValue:arr forKey:key];
        }

        if (self.sendPersonArray)
        {
            self.sendPersonArray(personArray);
        }
        if (self.sendPersonDictionary)
        {
            self.sendPersonDictionary(personDictionary);
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
        //NSLog(@"%@",error);
        
    }];
}

/**
 * 获取解说视频数组
 */
-(void)getVideoArrayByUID:(NSString *)dm_uid
{
    //生成URL
    NSString *urlString = kVideoURL(dm_uid);
    
    NSMutableArray *videoArray = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [myManager GET:urlString parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *array = dictionary[@"result"][@"video_list_by_time"];
        for (NSDictionary *dict in array) {
        
            LLT_VideoModel *videoModel = [[LLT_VideoModel alloc]init];
            [videoModel setValuesForKeysWithDictionary:dict];
            [videoArray addObject:videoModel];
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        //NSLog(@"%@",error);
    }];

    

    if (self.sendVideoArray) {
        self.sendVideoArray(videoArray);
    }
}


/**
 * @brief 把格式化的JSON字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
-(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    if (err) {
        //NSLog(@"json解析失败 : %@",err);
        return nil;
    }
    
    return dic;
    
}


@end
